package io.renren.modules.eightapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.dao.BaseDao;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.eightapp.dao.EiCostInfoDao;
import io.renren.modules.eightapp.dao.EiPayInfoDao;
import io.renren.modules.eightapp.dto.EiCostInfoDTO;
import io.renren.modules.eightapp.entity.EiCostInfoEntity;
import io.renren.modules.eightapp.entity.EiPayInfoEntity;
import io.renren.modules.eightapp.service.EiCostInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Mark 1132115960@gmail.com
 * @since 1.0.0 2022-12-27
 */
@Service
public class EiCostInfoServiceImpl extends CrudServiceImpl<EiCostInfoDao, EiCostInfoEntity, EiCostInfoDTO> implements EiCostInfoService {

    @Autowired
    private EiCostInfoDao eiCostInfoDao;
    @Autowired
    private EiPayInfoDao eiPayInfoDao;

    @Override
    public QueryWrapper<EiCostInfoEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<EiCostInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    @Transactional
    public void confirmPayment(Long id) {
        EiCostInfoEntity costInfo = eiCostInfoDao.selectById(id);
        EiPayInfoEntity payInfo = new EiPayInfoEntity();
        Date date = new Date();
        payInfo.setOwnerId(costInfo.getOwnerId());
        payInfo.setCostId(costInfo.getId());
        payInfo.setPayTime(date);
        payInfo.setTotal(costInfo.getTotal());
        payInfo.setMemo(costInfo.getType());
        payInfo.setCreator(costInfo.getCreator());
        payInfo.setCreateTime(date);
        payInfo.setState(1);
        eiPayInfoDao.insert(payInfo);
        eiCostInfoDao.deleteById(id);
    }

    @Override
    public List<EiCostInfoDTO> getCostInfoAllDataList() {
        LambdaQueryWrapper<EiCostInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select();
        List<EiCostInfoEntity> entityList = eiCostInfoDao.selectList(queryWrapper);
        List<EiCostInfoDTO> dtoList = ConvertUtils.sourceToTarget(entityList, EiCostInfoDTO.class);
        return dtoList;
    }

    @Override
    public List<EiCostInfoDTO> getCostInfoDataListByOwnerId(Integer ownerId) {
        LambdaQueryWrapper<EiCostInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select().eq(EiCostInfoEntity::getOwnerId, ownerId);
        List<EiCostInfoEntity> entityList = eiCostInfoDao.selectList(queryWrapper);
        List<EiCostInfoDTO> dtoList = ConvertUtils.sourceToTarget(entityList, EiCostInfoDTO.class);
        return dtoList;
    }

    @Override
    public List<EiCostInfoDTO> getCostInfoDataListByType(String type) {
        LambdaQueryWrapper<EiCostInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select().eq(EiCostInfoEntity::getType, type);
        List<EiCostInfoEntity> entityList = eiCostInfoDao.selectList(queryWrapper);
        List<EiCostInfoDTO> dtoList = ConvertUtils.sourceToTarget(entityList, EiCostInfoDTO.class);
        return dtoList;
    }
}